package com.jpsoft.shinestar.modules.base.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.dto.HolidayCompanyDTO;
import com.jpsoft.shinestar.modules.base.dto.HolidayCompanyInfoDTO;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.service.CompanyInfoService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.base.entity.HolidayCompany;
import com.jpsoft.shinestar.modules.base.service.HolidayCompanyService;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.mapping.ResultMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/base/holidayCompany")
public class HolidayCompanyController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private HolidayCompanyService holidayCompanyService;
    @Autowired
    private CompanyInfoService companyInfoService;

    @ApiOperation(value="添加信息")
    @PostMapping("add")
    public MessageResult<HolidayCompany> add(@ModelAttribute HolidayCompany holidayCompany){
        MessageResult<HolidayCompany> msgResult = new MessageResult<>();

        try {
            holidayCompany.setId(UUID.randomUUID().toString());

            int affectCount = holidayCompanyService.insert(holidayCompany);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(holidayCompany);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="获取信息")
    @GetMapping("edit/{id}")
    public MessageResult<List<HolidayCompanyDTO>> edit(@PathVariable("id") String id){
        MessageResult<List<HolidayCompanyDTO>> msgResult = new MessageResult<>();

        try {
            List<HolidayCompany> list = holidayCompanyService.findByHolidayId(id);

            List<HolidayCompanyDTO> holidayCompanyDTOList = new ArrayList<>();

            for (HolidayCompany holidayCompany:list) {
                HolidayCompanyDTO holidayCompanyDTO = new HolidayCompanyDTO();
                holidayCompanyDTO.setHolidayCompanyId(holidayCompany.getId());
                holidayCompanyDTO.setId(holidayCompany.getCompanyId());
                CompanyInfo companyInfo = companyInfoService.get(holidayCompany.getCompanyId());
                if(companyInfo!=null){
                    holidayCompanyDTO.setName(companyInfo.getName());
                }
                holidayCompanyDTO.setSubordinate(holidayCompany.getSubordinate());
                holidayCompanyDTOList.add(holidayCompanyDTO);
            }

            msgResult.setResult(true);
            msgResult.setData(holidayCompanyDTOList);
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="更新用户")
    @PostMapping("update")
    public MessageResult<Integer> update(@RequestBody HolidayCompanyInfoDTO holidayCompanyInfoDTO,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            Date now = new Date();

            List<HolidayCompany> list = new ArrayList<>();

            for (HolidayCompanyDTO dto:holidayCompanyInfoDTO.getTableJson()) {
                HolidayCompany holidayCompany = new HolidayCompany();
                holidayCompany.setId(UUID.randomUUID().toString());
                holidayCompany.setHolidayId(holidayCompanyInfoDTO.getHolidayId());
                holidayCompany.setCompanyId(dto.getId());
                holidayCompany.setSubordinate(dto.getSubordinate());
                holidayCompany.setCreateBy(subject);
                holidayCompany.setCreateTime(now);
                holidayCompany.setDelFlag(false);
                list.add(holidayCompany);
            }

            holidayCompanyService.updateAndInsert(list,holidayCompanyInfoDTO.getHolidayId(),subject,now);

            msgResult.setResult(true);

        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="删除用户")
    @PostMapping("delete/{id}")
    public MessageResult<HolidayCompany> delete(@PathVariable("id") String id,@RequestAttribute String subject){
        MessageResult<HolidayCompany> msgResult = new MessageResult<>();

        try {
            HolidayCompany holidayCompany = holidayCompanyService.get(id);

            holidayCompany.setDelFlag(true);
            holidayCompany.setUpdateBy(subject);
            holidayCompany.setUpdateTime(new Date());

            int affectCount = holidayCompanyService.update(holidayCompany);

            if (affectCount > 0) {
                msgResult.setResult(true);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }


    @ApiOperation(value="批量删除")
    @PostMapping("batchDelete/{id}")
    public MessageResult<HolidayCompany> batchDelete(@PathVariable("id") String id){
        MessageResult<HolidayCompany> msgResult = new MessageResult<>();

        msgResult.setResult(false);
        msgResult.setMessage("未实现");

        return msgResult;
    }

    @ApiOperation(value="列表")
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public MessageResult<Map> pageList(
            String id,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            HttpServletRequest request){
        String subject = (String)request.getAttribute("subject");

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time","desc"));

        if (StringUtils.isNotEmpty(id)) {
            searchParams.put("id","%" + id + "%");
        }

        Page<HolidayCompany> page = holidayCompanyService.pageSearch(searchParams,pageIndex,pageSize,sortList);

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }
}
