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

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.dto.CustomerDiningDTO;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.CompanyInfoService;
import com.jpsoft.shinestar.modules.base.service.CustomerDiningRecordService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
import com.jpsoft.shinestar.modules.business.dto.AdjustShiftListDTO;
import com.jpsoft.shinestar.modules.business.entity.AdjustShift;
import com.jpsoft.shinestar.modules.business.entity.AdjustShiftPerson;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.TypeNameUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author 墨鱼_mo
 * @date 2021-5-24 14:27
 */
@RestController
@RequestMapping("/mobile/customerDiningApi")
@Slf4j
public class CustomerDiningApiController {


    @Autowired
    private CustomerDiningRecordService customerDiningRecordService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;


    @ApiOperation(value = "来客接待进餐提交")
    @PostMapping(value = "customerDiningSubmit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerCompany", value = "客户单位", required = true, paramType = "form"),
            @ApiImplicitParam(name = "reason", value = "事由", required = true, paramType = "form"),
            @ApiImplicitParam(name = "customerNum", value = "客户人数", required = true, paramType = "form"),
            @ApiImplicitParam(name = "accompanyNum", value = "陪同人数", required = true, paramType = "form"),
            @ApiImplicitParam(name = "diningTime", value = "进餐时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "diningName", value = "用餐安排（午餐，晚餐，宵夜）", required = true, paramType = "form"),
            @ApiImplicitParam(name = "diningFee", value = "用餐标准金额", required = true, paramType = "form"),
            @ApiImplicitParam(name = "formId", value = "页面路径", required = true, paramType = "form"),
            @ApiImplicitParam(name = "draft", value = "状态（0：草稿，1：提交）", required = true, paramType = "form")
    })
    public MessageResult<Map> customerDiningSubmit(String ids,String draft,String customerCompany, String reason, Integer customerNum, Integer accompanyNum, String diningTime,String diningName,String diningFee,String formId,String token, @RequestAttribute String subject) {


        MessageResult<Map> messageResult = new MessageResult<>();
        try {

            PersonInfo person = personInfoService.get(Long.parseLong(subject));
            PersonPopedom personPopedom = personPopedomService.get(person.getPopedom());


            String id = customerDiningRecordService.customerDiningSubmit(personPopedom, customerCompany, reason,customerNum,accompanyNum,diningTime,diningName,diningFee,formId,ids,draft);

            Map<String, Object> map = new HashMap<>();
            map.put("id", id);
            messageResult.setData(map);
            messageResult.setCode(200);
            messageResult.setResult(true);


        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);

        }

        return messageResult;
    }



    @ApiOperation(value = "来客接待进餐提交记录")
    @PostMapping(value = "customerDiningList")
    public MessageResult<Map> customerDiningList(String token, @RequestAttribute String subject, @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {


        MessageResult<Map> messageResult = new MessageResult<>();
        try {

            PersonInfo person = personInfoService.get(Long.parseLong(subject));
            PersonPopedom personPopedom = personPopedomService.get(person.getPopedom());

            List<CustomerDiningDTO> list = new ArrayList<>();
            Map<String, Object> searchParams = new HashMap<>();
            searchParams.put("personPopedomId", personPopedom.getId());
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("create_time", "desc"));
            Page<CustomerDiningRecord> page = customerDiningRecordService.pageSearch(searchParams, pageIndex, pageSize, true, sortList);

            List<CustomerDiningRecord> customerDiningRecordList = page.getResult();
            if (customerDiningRecordList.size() > 0) {
                for (CustomerDiningRecord customerDiningRecord : customerDiningRecordList) {
                  CustomerDiningDTO customerDiningDTO = new CustomerDiningDTO();
                  BeanUtils.copyProperties(customerDiningRecord,customerDiningDTO);
                  //接待公司
                    CompanyInfo companyInfo = companyInfoService.findByIdIgnoreDel(customerDiningRecord.getCompanyId());
                    if (companyInfo != null){
                        customerDiningDTO.setCompanyName(companyInfo.getName());
                    }
                    //提交人
                    PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(customerDiningRecord.getPersonPopedomId());
                    PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                    if (personInfo1 != null){
                        customerDiningDTO.setPersonName(personInfo1.getName());
                    }

                    customerDiningDTO.setStatusName(TypeNameUtil.getExamineTypeName(customerDiningDTO.getStatus()));
                    list.add(customerDiningDTO);
                }
            }


            Map<String, Object> pageMap = new HashMap<>();
            pageMap.put("recordsTotal", page.getTotal());
            pageMap.put("recordsFiltered", page.getTotal());
            pageMap.put("totalPage", page.getPages());
            pageMap.put("pageNumber", page.getPageNum());
            pageMap.put("pageSize", page.getPageSize());
            pageMap.put("data", list);

            messageResult.setData(pageMap);
            messageResult.setCode(200);
            messageResult.setResult(true);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);

        }

        return messageResult;
    }


    @ApiOperation(value = "根据id查询来客接待进餐详情")
    @PostMapping(value = "customerDiningDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "form")
    })
    public MessageResult<Map> customerDiningDetail(String id, String token, @RequestAttribute String subject) {


        MessageResult<Map> messageResult = new MessageResult<>();
        try {

            PersonInfo person = personInfoService.get(Long.parseLong(subject));
            PersonPopedom personPopedom = personPopedomService.get(person.getPopedom());


            CustomerDiningRecord customerDiningRecord = customerDiningRecordService.get(id);

            //接待部门
            CompanyInfo companyInfo = companyInfoService.findByIdIgnoreDel(customerDiningRecord.getCompanyId());


            PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(customerDiningRecord.getPersonPopedomId());
            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());






            Map<String, Object> map = new HashMap<>();
            map.put("name", personInfo1.getName());
            map.put("faceImageUrl", personInfo1.getFaceImageUrl());
            map.put("status", customerDiningRecord.getStatus());
            map.put("statusName", TypeNameUtil.getExamineTypeName(customerDiningRecord.getStatus()));
            map.put("companyName", companyInfo.getName());
            Week week =  DateUtil.dayOfWeekEnum(customerDiningRecord.getCreateTime());
            map.put("createTime", DateUtil.format(customerDiningRecord.getCreateTime(), "yyyy-MM-dd") + " " + week.toChinese());
            map.put("diningTime", DateUtil.format(customerDiningRecord.getDiningTime(), "yyyy-MM-dd HH:mm:ss"));
            map.put("reason",customerDiningRecord.getReason());
            map.put("customerCompany",customerDiningRecord.getCustomerCompany());
            map.put("customerNum",customerDiningRecord.getCustomerNum());
            map.put("accompanyNum",customerDiningRecord.getAccompanyNum());
            map.put("diningName",customerDiningRecord.getDiningName());
            map.put("diningFee",customerDiningRecord.getDiningFee());
            map.put("diningFeeChinese", Convert.digitToChinese(customerDiningRecord.getDiningFee()));
            messageResult.setData(map);
            messageResult.setCode(200);
            messageResult.setResult(true);

        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);

        }
        return messageResult;
    }



}
