package com.foreverwin.mesnac.dispatch.controller;

import com.foreverwin.mesnac.dispatch.dto.ProductionCalendarDTO;
import com.foreverwin.mesnac.dispatch.model.ProductionCalendar;
import com.foreverwin.mesnac.dispatch.model.ProductionDayType;
import com.foreverwin.mesnac.dispatch.service.ProductionCalendarService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 *
 * @author syngna
 * @since 2020-08-18
 */
@RestController
@RequestMapping("/production-calendar")
public class ProductionCalendarController {

    @Autowired
    public ProductionCalendarService productionCalendarService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getProductionCalendarById(@PathVariable String id) {
        return R.ok( productionCalendarService.getById(id));
    }

    /**
     * 根据工作中心、年、月检索工作日历数据
     * @param workCenter
     * @param year
     * @param month
     * @return
     */
    @ResponseBody
    @GetMapping("/{workCenter}/{year}/{month}")
    public R getProductionCalendarById(@PathVariable("workCenter")String workCenter, @PathVariable("year")String year, @PathVariable("month")String month) {
        String site = CommonMethods.getSite();
        return R.ok( productionCalendarService.listByWorkCenterYearMonth(site, workCenter, year, month));
    }

    /**
     * 新增或修改
     * @param productionCalendarList  传递的实体列表
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R saveOrUpdate(@RequestBody List<ProductionCalendarDTO> productionCalendarList) {
        String user = CommonMethods.getUser();
        String site = CommonMethods.getSite();
        List<ProductionCalendar> saveParam = new ArrayList<>();
        for(ProductionCalendarDTO productionCalendar : productionCalendarList) {
            String workCenter = productionCalendar.getWorkCenter();
            String dateId = productionCalendar.getDateId();
            productionCalendar.setHandle(ProductionCalendar.genHandle(site, workCenter, dateId));
            productionCalendar.setSite(site);
            productionCalendar.setCreator(user);
            productionCalendar.setUpdater(user);
            ProductionCalendar pc = new ProductionCalendar();
            BeanUtils.copyProperties(productionCalendar, pc);
            pc.setProductionDayTypeBo(ProductionDayType.genHandle(site, productionCalendar.getProductionDayType()));
            saveParam.add(pc);
        }
        return R.ok(productionCalendarService.saveOrUpdateBatch(saveParam, 1000));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(productionCalendarService.removeById(id));
    }

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