package com.dgtly.web.controller.company;

import java.util.Date;
import java.util.List;

import com.dgtly.common.constant.UserConstants;
import com.dgtly.common.core.domain.Ztree;
import com.dgtly.common.utils.security.EncryptPassWordClass;
import com.dgtly.companyext.domain.CompanyExamine;
import com.dgtly.companyext.domain.CompanyReviewed;
import com.dgtly.companyext.service.ICompanyExamineService;
import com.dgtly.companyext.service.ICompanyReviewedService;
import com.dgtly.framework.util.ShiroUtils;
import com.dgtly.system.domain.SysDept;
import com.dgtly.system.domain.SysUser;
import com.dgtly.system.service.ISysDeptService;
import com.dgtly.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dgtly.common.annotation.Log;
import com.dgtly.common.enums.BusinessType;
import com.dgtly.system.domain.SysCompany;
import com.dgtly.system.service.ISysCompanyService;
import com.dgtly.common.core.controller.BaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.utils.poi.ExcelUtil;
import com.dgtly.common.core.page.TableDataInfo;
import org.springframework.web.servlet.ModelAndView;

/**
 * 公司表Controller
 * 
 * @author dgtly
 * @date 2020-01-08
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController
{
    private String prefix = "company";

    @Autowired
    private ISysCompanyService sysCompanyService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysUserService userService;


    @Autowired
    private ICompanyReviewedService companyReviewedService;

    @Autowired
    private ICompanyExamineService companyExamineService;

    @RequiresPermissions("company:company:view")
    @GetMapping("/company")
    public String company()
    {
        return prefix + "/company";
    }

    /**
     * 查询公司表列表
     */
    @RequiresPermissions("company:company:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysCompany sysCompany)
    {
        startPage();
        List<SysCompany> list = sysCompanyService.selectSysCompanyListExRoot(sysCompany);
        return getDataTable(list);
    }

    /**
     * 查询公司表列表
     */
    @GetMapping("/tree")
    public Object select(SysCompany sysCompany)
    {
        return prefix + "/tree";
    };
    /**
     * 查询公司表列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public Object treeData(SysCompany sysCompany)
    {
        List<Ztree> list = sysCompanyService.selectSysCompanyTree(sysCompany);
        return list;
    }



    /**
     * @descption: 查看公司详情
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020-2-25
     */
    @RequiresPermissions("company:conpmay:info")
    @GetMapping("/info/{id}")
    public String info(@PathVariable("id")Long id, ModelMap mmap)
    {
        mmap.put("sysCompany",sysCompanyService.selectSysCompanyById(id));
        return prefix + "/info";
    }


    /**
     * @descption: 商户入驻审核
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020-2-25
     */
    @RequiresPermissions("company:enterexamine:view")
    @GetMapping("/enterexamine")
    public String enterexamine()
    {
        return prefix + "/enterexamine/reviewed";
    }

    /**
     * 查询商户入驻信息审核列表
     */
    @RequiresPermissions("company:enterexamine:list")
    @PostMapping("/enterexamine/list")
    @ResponseBody
    public TableDataInfo list(CompanyReviewed companyReviewed)
    {
        startPage();
        companyReviewed.setReason("0");
        List<CompanyReviewed> list = companyReviewedService.selectCompanyReviewedListWiteAudit(companyReviewed);
        return getDataTable(list);
    }

    /**
     * @descption: 商户入驻审核
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020-2-25
     */
    @RequiresPermissions("company:enterexamine:approve")
    @GetMapping("/enterexamine/approvePage/{id}")
    public String approvePage(@PathVariable("id") Long id, ModelMap mmap)
    {
        CompanyReviewed companyReviewed = companyReviewedService.selectCompanyReviewedById(id);
        mmap.put("companyReviewed", companyReviewed);
        return prefix + "/enterexamine/approve";
    }

    /**
     * 审核商户入驻
     */
    @RequiresPermissions("companyext:enterexamine:approve")
    @Log(title = "审核商户入驻", businessType = BusinessType.UPDATE)
    @PostMapping("/enterexamine/approve")
    @ResponseBody
    public AjaxResult approve(CompanyExamine companyExamine)
    {
        companyExamine.setCreateBy(ShiroUtils.getLoginName());
        int i =0;
        if(companyExamine.getStatus().equals(1)){
            /*审核通过*/
            i=sysCompanyService.allowEnter(companyExamine);

        }else{
            /*审核拒绝*/
            i=companyReviewedService.refuse(companyExamine);
        }
        return toAjax(i);
    }



    /*****************************审核商户信息提交相关*************************************************/

    /**
     * @descption: 商户修改审核
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020-2-25
     */
    @RequiresPermissions("company:editexamine:view")
    @GetMapping("/editexamine")
    public String editexamine()
    {
        return prefix + "/editexamine/reviewed";
    }


    /**
     * @descption: 查询商户修改信息审核列表
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020/3/3 14:03
     */
    @RequiresPermissions("company:editexamine:list")
    @PostMapping("/editexamine/list")
    @ResponseBody
    public TableDataInfo editexamine(CompanyReviewed companyReviewed)
    {
        startPage();
        companyReviewed.setReason("1");
        List<CompanyReviewed> list = companyReviewedService.selectCompanyReviewedListWiteAudit(companyReviewed);
        return getDataTable(list);
    }
    /**
     * @descption: 审核商户信息修改
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020-2-25
     */
    @RequiresPermissions("company:editexamine:approvePage")
    @GetMapping("/editexamine/approvePage/{id}")
    public String audit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CompanyReviewed companyReviewed = companyReviewedService.selectCompanyReviewedById(id);
        SysCompany sysCompany = sysCompanyService.selectSysCompanyById(companyReviewed.getCompanyId());
        mmap.put("companyReviewed", companyReviewed);
        mmap.put("sysCompany", sysCompany);
        return prefix + "/editexamine/approve";
    }

    /**
     * 审核商户入驻
     */
    @RequiresPermissions("companyext:editexamine:approve")
    @Log(title = "审核商户入驻", businessType = BusinessType.UPDATE)
    @PostMapping("/editexamine/approve")
    @ResponseBody
    public AjaxResult editapprove(CompanyExamine companyExamine)
    {
        companyExamine.setCreateBy(ShiroUtils.getLoginName());
        int i =0;
        if(companyExamine.getStatus().equals(1)){
            /*审核通过*/
            i=sysCompanyService.allowEdit(companyExamine);

        }else{
            /*审核拒绝*/
            i=companyReviewedService.refuse(companyExamine);
        }
        return toAjax(i);
    }




    /**************************************商户查看自身信息相关*****************************************************/
    /**
     * @descption: 商户信息查看
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020/3/3 14:03
     */
    @RequiresPermissions("company:edit:view")
    @GetMapping("/edit")
    public String edit(ModelMap mmp)
    {
        Long companyId = ShiroUtils.getCompanyId();
        SysCompany sysCompany = sysCompanyService.selectSysCompanyById(companyId);

        mmp.addAttribute("sysCompany",sysCompany);
        return prefix + "/edit/edit";
    }

    /**
     * @descption: 商户信息申请修改
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020/3/3 14:03
     */
    @RequiresPermissions("company:edit:submit")
    @PostMapping("/edit/submit")
    @ResponseBody
    public Object submit(CompanyReviewed companyReviewed)
    {
        companyReviewed.setReason("1");
        companyReviewed.setStatus("0");
        int i =  companyReviewedService.insertCompanyReviewed(companyReviewed);
        return toAjax(i);
    }

    /**
     * @descption: 查看商户信息修改历史
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020/3/3 14:03
     */
    @RequiresPermissions("company:edit:infoHistory")
    @GetMapping("/edit/infoHistory")
    public Object infoHistory(ModelMap mmp)
    {
        CompanyReviewed cr = new CompanyReviewed();
        cr.setCompanyId(ShiroUtils.getCompanyId());
        List<CompanyReviewed> crs =  companyReviewedService.selectCompanyReviewedListById(cr);
        mmp.addAttribute("list",crs);
        return prefix + "/edit/infoHistory";
    }


    /**
     * @descption: 商户修改详情
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020-2-25
     */
    @GetMapping("/edit/historyInfo/{id}")
    public String historyInpo(@PathVariable("id") Long id, ModelMap mmap)
    {
        CompanyReviewed companyReviewed = companyReviewedService.selectCompanyReviewedById(id);
        mmap.put("companyReviewed", companyReviewed);
        return prefix + "/edit/historyInfo";
    }


}
