package xiaoqu.home.open.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import xiaoqu.home.open.aop.AvoidRepeatSubmit;
import xiaoqu.home.open.controller.input.DistrictIdInput;
import xiaoqu.home.open.controller.input.LabelManagePageInput;
import xiaoqu.home.open.controller.insert.LabelManageInsertInput;
import xiaoqu.home.open.controller.output.LabelManageOutput;
import xiaoqu.home.open.model.db2.LabelManage;
import xiaoqu.home.open.model.db2.UserToLabel;
import xiaoqu.home.open.publicBean.BaseOutput;
import xiaoqu.home.open.publicBean.IdEntity;
import xiaoqu.home.open.publicBean.PageOutput;
import xiaoqu.home.open.service.db2.LabelManageService;
import xiaoqu.home.open.service.db2.UserToLabelService;
import xiaoqu.home.open.utils.ResultResponseBeanUtil;
import xiaoqu.home.open.utils.WrapperUtils;

@Controller
@RequestMapping(value = "/label")
@Api(value = "标签管理", description = "标签管理")
public class LabelManageController {
	@Autowired
	private LabelManageService labelManageService;
	@Autowired
	private UserToLabelService userToLabelService;

	@ApiOperation(value = "分页查询")
	@ResponseBody
	@RequestMapping(value = "/pageList", method = RequestMethod.POST)
	public BaseOutput<PageOutput<LabelManageOutput>> pageList(HttpServletRequest request, @RequestBody LabelManagePageInput input) {
		PageOutput<LabelManageOutput> output = new PageOutput<LabelManageOutput>();
		Long districtId = input.getDistrictId();
		if (districtId == null || districtId == 0) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		QueryWrapper<LabelManage> wrapper = WrapperUtils.getWrapper(false);
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		Page<LabelManage> pageList = new Page<LabelManage>(input.getPage(), input.getRows());
		List<LabelManage> entityList = labelManageService.page(pageList, wrapper).getRecords();
		List<LabelManageOutput> outList = new ArrayList<LabelManageOutput>();
		for (LabelManage bean : entityList) {
			LabelManageOutput outputBean = new LabelManageOutput();
			BeanUtils.copyProperties(bean, outputBean);
			outList.add(outputBean);
		}
		output.setPage(pageList.getCurrent());
		output.setRecords(pageList.getTotal());
		output.setTotal(pageList.getPages());
		output.setRows(outList);
		return ResultResponseBeanUtil.success(output);
	}
	@ApiOperation(value = "所有的标签")
	@ResponseBody
	@RequestMapping(value = "/allLabels", method = RequestMethod.POST)
	public BaseOutput<List<LabelManage>> allSkills(HttpServletRequest request, @RequestBody DistrictIdInput input) {
		Long districtId = input.getDistrictId();
		if (districtId == null || districtId == 0) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		QueryWrapper<LabelManage> wrapper = WrapperUtils.getWrapper(false);
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		wrapper.orderByDesc("create_time");
		List<LabelManage> entityList = labelManageService.list( wrapper);
		return ResultResponseBeanUtil.success(entityList);
	}


	@ApiOperation(value = "删除")
	@ResponseBody
	@AvoidRepeatSubmit
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public BaseOutput<String> delete(HttpServletRequest request, @RequestBody IdEntity input) {

		if (input.getId() != null) {
			LabelManage labelManage = labelManageService.getById(input.getId());
			if (labelManage != null) {
				// 判断是否有使用
				QueryWrapper<UserToLabel> wrapper = WrapperUtils.getWrapper(false);
				wrapper.eq("label_id", input.getId());
				int count = userToLabelService.count(wrapper);
				if (count > 0) {
					return ResultResponseBeanUtil.fail("已经被使用，不能删除");
				}
				labelManage.setSoftDelete(1);
				labelManageService.updateById(labelManage);
			} else {
				return ResultResponseBeanUtil.fail("当前记录未找到");
			}
		}
		return ResultResponseBeanUtil.success("删除成功");

	}

	@ApiOperation(value = "新增或更新")
	@ResponseBody
	@AvoidRepeatSubmit
	@RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
	public BaseOutput<String> addOrUpdate(HttpServletRequest request, @RequestBody LabelManageInsertInput input) {
		if (input.getId()!=null) {
			LabelManage labelManage = labelManageService.getById(input.getId());
			if (!labelManage.getLabelName().equals(input.getLabelName())) {
				QueryWrapper<LabelManage> wrapperB = WrapperUtils.getWrapper(false);
				wrapperB.eq("label_name", input.getLabelName());
				wrapperB.eq("district_id", input.getDistrictId());
				int countB = labelManageService.count(wrapperB);
				if (countB > 0) {
					return ResultResponseBeanUtil.fail("记录已存在，请修改后再提交");
				}
			}
			BeanUtils.copyProperties(input, labelManage);
			labelManageService.updateById(labelManage);
		}else {
			LabelManage labelManage = new LabelManage();
			QueryWrapper<LabelManage> wrapperB = WrapperUtils.getWrapper(false);
			wrapperB.eq("label_name", input.getLabelName());
			wrapperB.eq("district_id", input.getDistrictId());
			int countB = labelManageService.count(wrapperB);
			if (countB > 0) {
				return ResultResponseBeanUtil.fail("记录已存在，请修改后再提交");
			}
			BeanUtils.copyProperties(input, labelManage);
			labelManageService.save(labelManage);
		}
		return ResultResponseBeanUtil.success("操作成功");

	}

}
