package com.foreverwin.mesnac.dispatch.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.foreverwin.mesnac.dispatch.model.ProductionCalendar;
import com.foreverwin.mesnac.dispatch.model.ProductionDayType;
import com.foreverwin.mesnac.dispatch.model.WorkShift;
import com.foreverwin.mesnac.dispatch.service.ProductionCalendarService;
import com.foreverwin.mesnac.dispatch.service.ProductionDayTypeService;
import com.foreverwin.mesnac.dispatch.service.WorkShiftService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author syngna
 * @since 2020-08-18
 */
@RestController
@RequestMapping("/production-day-type")
public class ProductionDayTypeController {

    @Autowired
    public ProductionDayTypeService productionDayTypeService;

    @Autowired
    public WorkShiftService workShiftService;

    @Autowired
    public ProductionCalendarService productionCalendarService;

    /**
    * 根据code查询
    *
    * @param code 工作日类型代码
    * @return
    */
    @ResponseBody
    @GetMapping("/{code}")
    public R getProductionDayTypeById(@PathVariable String code) {
        String site = CommonMethods.getSite();
        ProductionDayType productionDayType = productionDayTypeService.getOne(Wrappers.<ProductionDayType>lambdaQuery()
                .eq(ProductionDayType::getProductionDayType, code)
                .eq(ProductionDayType::getSite, site), false);
        if(productionDayType != null) {
            productionDayType.setWorkShiftList(workShiftService.list(Wrappers.<WorkShift>lambdaQuery().eq(WorkShift::getProductionDayTypeBo, productionDayType.getHandle())));
        }
        return R.ok(productionDayType);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getProductionDayTypeList(ProductionDayType productionDayType){
        List<ProductionDayType> result;
        String site = CommonMethods.getSite();
        productionDayType.setSite(site);
        QueryWrapper<ProductionDayType> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(productionDayType);
        result = productionDayTypeService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<ProductionDayType> frontPage, ProductionDayType productionDayType){
        IPage result;
        String site = CommonMethods.getSite();
        productionDayType.setSite(site);
        QueryWrapper<ProductionDayType> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(productionDayType);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            // modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(ProductionDayType::getHandle, frontPage.getGlobalQuery())
                .or().like(ProductionDayType::getSite, frontPage.getGlobalQuery())
                .or().like(ProductionDayType::getProductionDayType, frontPage.getGlobalQuery())
                .or().like(ProductionDayType::getDescription, frontPage.getGlobalQuery())
                .or().like(ProductionDayType::getColor, frontPage.getGlobalQuery())
                .or().like(ProductionDayType::getCreator, frontPage.getGlobalQuery())
                .or().like(ProductionDayType::getUpdater, frontPage.getGlobalQuery())
    );
        }
        result = productionDayTypeService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param productionDayType  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping("/save")
    public R save(@RequestBody ProductionDayType productionDayType) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        productionDayType.setHandle(ProductionDayType.genHandle(site, productionDayType.getProductionDayType()));
        productionDayType.setSite(site);
        productionDayType.setCreator(user);
        productionDayType.setUpdater(user);
        return R.ok(productionDayTypeService.save(productionDayType));
    }

    /**
     * 修改
     * @param productionDayType  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping("/update")
    public R updateById(@RequestBody ProductionDayType productionDayType) {
		String user = CommonMethods.getUser();
        String site = CommonMethods.getSite();
        productionDayType.setSite(site);
        productionDayType.setUpdater(user);
        if(StrUtil.isBlank(productionDayType.getHandle())) {
            productionDayType.setHandle(ProductionDayType.genHandle(site, productionDayType.getProductionDayType()));
        }
        return R.ok(productionDayTypeService.updateById(productionDayType));
    }

    /**
     * 根据id删除对象
     * @param code
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{code}")
    public R removeByCode(@PathVariable("code") String code){
        String site = CommonMethods.getSite();
        String handle = ProductionDayType.genHandle(site, code);
        int count = productionCalendarService.count(Wrappers.<ProductionCalendar>lambdaQuery().eq(ProductionCalendar::getProductionDayTypeBo, handle));
        if(count > 0) {
            return R.failed("工作日类型[" + code + "]已被生产日历引用，不能删除");
           // throw new Exception("工作日类型[" + code + "]已被生产日历引用，不能删除");
        }
        return R.ok(productionDayTypeService.removeById(handle));
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete-batch")
    public R removeByIds(List<String> ids){
        return R.ok(productionDayTypeService.removeByIds(ids));
    }
}