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.SkillManagePageInput;
import xiaoqu.home.open.controller.insert.SkillManageInsertInput;
import xiaoqu.home.open.controller.output.SkillManageOutput;
import xiaoqu.home.open.model.db2.SkillManage;
import xiaoqu.home.open.model.db2.UserToSkill;
import xiaoqu.home.open.publicBean.BaseOutput;
import xiaoqu.home.open.publicBean.IdEntity;
import xiaoqu.home.open.publicBean.PageOutput;
import xiaoqu.home.open.service.db2.SkillManageService;
import xiaoqu.home.open.service.db2.UserToSkillService;
import xiaoqu.home.open.utils.ResultResponseBeanUtil;
import xiaoqu.home.open.utils.WrapperUtils;

@Controller
@RequestMapping(value = "/skill")
@Api(value = "技能管理", description = "技能管理")
public class SkillController {
	@Autowired
	private UserToSkillService userToSkillService;
	@Autowired
	private SkillManageService skillManageService;

	@ApiOperation(value = "分页查询")
	@ResponseBody
	@RequestMapping(value = "/pageList", method = RequestMethod.POST)
	public BaseOutput<PageOutput<SkillManageOutput>> pageList(HttpServletRequest request, @RequestBody SkillManagePageInput input) {
		PageOutput<SkillManageOutput> output = new PageOutput<SkillManageOutput>();
		Long districtId = input.getDistrictId();
		if (districtId == null || districtId == 0) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		QueryWrapper<SkillManage> wrapper = WrapperUtils.getWrapper(false);
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		Page<SkillManage> pageList = new Page<SkillManage>(input.getPage(), input.getRows());
		List<SkillManage> entityList = skillManageService.page(pageList, wrapper).getRecords();
		List<SkillManageOutput> outList = new ArrayList<SkillManageOutput>();
		for (SkillManage bean : entityList) {
			SkillManageOutput outputBean = new SkillManageOutput();
			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 = "/allSkills", method = RequestMethod.POST)
	public BaseOutput<List<SkillManage>> allSkills(HttpServletRequest request, @RequestBody DistrictIdInput input) {
		Long districtId = input.getDistrictId();
		if (districtId == null || districtId == 0) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		QueryWrapper<SkillManage> wrapper = WrapperUtils.getWrapper(false);
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		wrapper.orderByDesc("create_time");
		List<SkillManage> entityList = skillManageService.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) {
			SkillManage skillManage = skillManageService.getById(input.getId());
			if (skillManage != null) {
				// 判断是否有使用
				QueryWrapper<UserToSkill> wrapper = WrapperUtils.getWrapper(false);
				wrapper.eq("skill_id", input.getId());
				int count = userToSkillService.count(wrapper);
				if (count > 0) {
					return ResultResponseBeanUtil.fail("已经被使用，不能删除");
				}
				skillManage.setSoftDelete(1);
				skillManageService.updateById(skillManage);
			} else {
				return ResultResponseBeanUtil.fail("当前记录未找到");
			}
		}
		return ResultResponseBeanUtil.success("删除成功");

	}

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

	}

}
